package cn.itcast.service.impl;

import cn.itcast.mapper.ClazzMapper;
import cn.itcast.pojo.Clazz;
import cn.itcast.pojo.PageInfo;
import cn.itcast.pojo.QuaryParma;
import cn.itcast.service.ClazzService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    //添加
    @Override
    public boolean addClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        int add = clazzMapper.addClazz(clazz);
        return add > 0;
    }

    //查询
    @Override
    public Clazz findById(Integer id) {
        Clazz clazzs = clazzMapper.findById(id);
        return clazzs;
    }

    //修改
    @Override
    public boolean upDate(Clazz clazz) {
        int row = clazzMapper.upDate(clazz);
        return row > 0;
    }

    //删除
    @Override
    public boolean deleteById(int id) {
        int row = clazzMapper.deleteById(id);
        return row > 0;
    }

    //分页查询
    @Override
    public PageInfo findByPage(QuaryParma quaryParma) {
        //开启分页
        Page<Clazz> page = PageHelper.startPage(quaryParma.getPage(), quaryParma.getPageSize());

        List<Clazz> list = clazzMapper.findByPage(quaryParma);

        PageInfo<Clazz> pageInfo1 = new PageInfo<>((int) page.getTotal(), list);

        return pageInfo1;

    }


    @Override
    public List<Clazz> findAll() {
        List<Clazz> clazz = clazzMapper.findAll();
        return clazz;
    }
}
